package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import properties.Properties;

/**
 * Class for creating a connection with the database. It is a singleton, so
 * there is at any point in time only one connection.
 * 
 * @author Project group 9
 * 
 */
public class DBConnection {

	private Connection connection = null;
	private static DBConnection dbConnection = null;
	
	/**
	 * Create a connection. 
	 */
	private DBConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection(Properties.getProperties().getUrl(),
					Properties.getProperties().getUser(), "");
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("Database connection established");
	}

	public static DBConnection getDBConnection() {
		if (dbConnection == null) {
			dbConnection = new DBConnection();
		}
		return dbConnection;
	}

	public Connection getConnection() {
		return connection;
	}
}
